<!-- 外购入库采购入库单  purchase-receipt -->
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="88px"
    >
      <el-form-item label="单据编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="物料长代码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入物料长代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表：已修复合计行计算逻辑 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      row-key="id"
      :default-expand-all="isExpandAll"
      v-if="refreshTable"
    >
      <el-table-column
        label="日期"
        align="center"
        prop="inTime"
        :formatter="dateFormatter2"
        min-width="120"
      />
      <el-table-column label="单据编号" min-width="200" align="center" prop="no" />
      <el-table-column label="供应商" align="center" min-width="160" prop="supplierName" />
      <el-table-column label="收料仓库" align="center" min-width="120" prop="warehouseName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="物料长代码" align="center" prop="barCode" width="120" />
      <el-table-column label="物料名称" align="center" prop="productName" min-width="120" />
      <el-table-column label="规格型号" align="center" prop="productStandard" />
      <el-table-column label="单位" align="center" prop="productUnitName" />
      <el-table-column label="实收数量" align="center" prop="realCount" min-width="120" />
      <el-table-column label="单位成本" align="center" prop="productPrice" />
      <el-table-column label="成本" align="center" prop="totalPrice" min-width="120" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="应收数量" align="center" prop="shouldCount" min-width="120" />
      <el-table-column label="制单" align="center" prop="userName" min-width="120" />
      <el-table-column label="审核标志" align="center" prop="status" />
      <!-- <el-table-column label="审核人" align="center" prop="auditor" />
      <el-table-column label="一级审核人" align="center" prop="firstLevelAudit" width="110" />
      <el-table-column label="二级审核人" align="center" prop="secondLevelAudit" width="110" />
      <el-table-column label="关联标志" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="打印次数" align="center" prop="printCount" />
      <el-table-column label="用途" align="center" prop="purpose" /> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { FinanceApi } from '@/api/finance/index'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 总条数
const queryParams = reactive({
  no: undefined,
  barCode: undefined,
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const isExpandAll = ref(true) // 是否展开，默认全部展开
const refreshTable = ref(true) // 重新渲染表格状态

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await FinanceApi.baseStockInPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  if (queryFormRef.value) {
    queryFormRef.value.resetFields()
  }
  Object.assign(queryParams, {
    no: undefined,
    barCode: undefined,
    pageNo: 1,
    pageSize: 10
  })
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    await message.exportConfirm()
    exportLoading.value = true
    const data = await FinanceApi.exportBaseStockInExcel(queryParams)
    download.excel(data, '外购入库采购单.xls')
  } catch (error) {
    console.error('导出失败:', error)
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped>
.ContentWrap {
  margin: 16px;
}

.el-table {
  width: 100%;
  margin-top: 16px;
}

.el-pagination {
  margin-top: 16px;
  text-align: right;
}

/* 合计行样式优化 */
.el-table__footer-wrapper .el-table__row {
  background-color: #f5f7fa;
  font-weight: 600;
}
</style>